// home/03list/order.js
var co = require('../../js/common.js')
var MAP = require('../../js/map.js')
var _this, app = getApp()
Page({
   data: {
      orderList: [],
      classList: {
         a: ' selected',
         b: ' unselected',
         c: ' unselected'
      },
      noOrderTips: '招师中'
   },

   onLoad: function (opt) {
      _this = this
      var locFunc = function (res) {
         console.log('【获取用户坐标locFunc】', res)
         if (res.errMsg == 'getLocation:ok') {
            app.aData.p = co.addValueForKey({}, res, ['latitude', 'longitude'])
         }
         _this.getOrdersNearby()
      }
      MAP.wxGetLocation(locFunc)
   },

   getOrdersNearby: function (e) {

      if (e) this.reSetData(e.target.id)

      var param = co.addValueForKey({}, app.aData.p, ['latitude', 'longitude'])
      // param.status = 'positive_int'
      param.state = co.empty(e) ? 'a' : e.target.id
      co.getSelfApi('order/Mg/getOrderList', param, function (res) {
         _this.setData({ orderList: res.data })
         console.log('【this.data】', _this.data, '【res】', res)
      })
   },

   reSetData: function (id) {
      var class_list = {
         a: ' unselected',
         b: ' unselected',
         c: ' unselected'
      }
      class_list[id] = ' selected'
      var tips
      if (id == 'a') {
         tips = '招师中'
      } else if (id == 'b') {
         tips = '上门中'
      } else if (id == 'c') {
         tips = '维修中'
      }
      this.setData({ classList: class_list, noOrderTips: tips })
   },

   toDetail: function (e) {
      var order = this.data.orderList[e.currentTarget.id]
      wx.navigateTo({
         url: '/home/04order/detail?order_id=' + order.order_id
      })
   },

   toCallMaster: function () {
      wx.navigateTo({ url: '../02callMaster/callMaster' })
   },

   toDialog: function (e) {
      var order = this.data.orderList[e.currentTarget.id]
      var self_uid = app.aData.userInfo.unionid
      if (order.creator_uid == self_uid) {
         this.gotoDialog(order.order_id)
         return
      }
      var param = {
         order_id: order.order_id,
         unionid: self_uid
      }
      co.getSelfApi('order/Get/isGotThisOrder', param, function (res) {
         //检查是否争取过订单
         if (res.code == 0) {
            _this.gotoDialog(order.order_id)
         } else {
            _this.unGotOrderModal(order.order_id)
         }
      })
   },

   gotoDialog: function (o_id) {
      wx.navigateTo({ url: '/home/04order/dialog?order_id=' + o_id })
   },
   //未争取此订单的提示
   unGotOrderModal: function (o_id) {
      wx.showModal({
         title: '温馨提示',
         content: '先争取此订单，才能进入业主对话哦！',
         showCancel: true,
         cancelText: '取消',
         cancelColor: '',
         confirmText: '现在争取',
         confirmColor: '#f60',
         success: function (res) {
            if (res.confirm) {
               wx.navigateTo({
                  url: '/home/04order/detail?order_id=' + o_id
               })
            }
         },
         fail: function (res) { }
      })
   },
   //分享
   onShareAppMessage: function () {
      var urlParam = ''
      if (app.aData.userInfo.openid) {
         urlParam = '?inviter_oid=' + app.aData.userInfo.openid + '&inviter_uid=' + app.aData.userInfo.unionid
      }
      var share = {
         title: '【门业维修共享平台】',
         path: '/home/01homePage/home' + urlParam
      }
      console.log('【分享】share', share)
      return share
   },
})